package cn.edu.ctbu.demo1.webapi;

import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Tc;
import cn.edu.ctbu.demo1.service.TcService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@RequestMapping("/webapi/tc")
public class TcRestController {
    @Autowired
    TcService tcService;
    /**
     * 读取的url:/webapi/tc/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Tc tc = tcService.findById(id);
        return RUtil.success(tc);
    }

    /**
     * 新增学生的方法
     * @param tc
     * @return
     */
    @PostMapping("/insert")
    public R insert(Tc tc){

        Tc tc1 = tcService.insert(tc);
        return RUtil.success(tc1);
    }

    /**
     * 更新学生的方法
     * @param tc
     * @return
     */
    @PutMapping("/update")
    public R update(Tc tc){
        tcService.update(tc);
        return RUtil.success(tc);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        tcService.delete(id);
        return RUtil.success(null);
    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="select",defaultValue = "")Integer select,
            @RequestParam(value ="content",defaultValue = "")String content
    ){
        Page<Tc> tcPage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"id"));
            tcPage = tcService.findAll(pageable);
        }
        else {
            Tc tc = new Tc();
            Pageable pageable = PageRequest.of(0,10, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher = null;
            switch (select) {
                case 1:
                    tc.setNo(content);
                    matcher = ExampleMatcher.matching().
                            withMatcher("no", ExampleMatcher.GenericPropertyMatchers.contains());
                    break;
                case 2:
                    tc.setCname(content);
                    matcher = ExampleMatcher.matching()//模糊查询
                            .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains());
                    break;
                case 3:
                    tc.setTname(content);
                    matcher = ExampleMatcher.matching()//模糊查询
                            .withMatcher("tname", ExampleMatcher.GenericPropertyMatchers.contains());
                    break;
            }

            Example<Tc> example = Example.of(tc,matcher);
            tcPage = tcService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(tcPage.getContent(),tcPage.getTotalElements());
        return RUtil.success(pageUtils);
    }

}
